package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.widget.MyDialog;
import com.jjwxc.jwjskandriod.model.BookPurchaseResponse;
import com.jjwxc.jwjskandriod.model.UserBalanceResponse;
import com.jjwxc.jwjskandriod.model.UserProfileRequest;
import com.jjwxc.jwjskandriod.readActivity.utils.Constant;
import com.jjwxc.jwjskandriod.readActivity.utils.FileUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.SharedPreUtils;

public class AccountDelActivity extends BaseActivity {

    private TextView tv_userInfo;
    private TextView tv_wjb;
    private TextView tv_time;
    private TextView tv_buy;
    private TextView dialog_no;
    private TextView dialog_ok;

    @Override
    public int getContentViewId() {
        return R.layout.activity_accountdel;
    }

    @Override
    public void findView() {
        setTitle("账号注销");

        tv_userInfo = findViewById(R.id.tv_userInfo);
        tv_wjb = findViewById(R.id.tv_wjb);
        tv_time = findViewById(R.id.tv_time);
        tv_buy = findViewById(R.id.tv_buy);
        dialog_no = findViewById(R.id.dialog_no);
        dialog_ok = findViewById(R.id.dialog_ok);
    }

    @Override
    public void setListener() {
        dialog_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        dialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyDialog(AccountDelActivity.this, "你确定要注销账号吗？", "确定", "取消", new MyDialog.DialogClickListener() {
                    @Override
                    public void onOkClick(Dialog dialog) {
                        post(Url.deleteUser, "注销账号中。。。", String.class, new FFNetWorkCallBack<String>() {
                            @Override
                            public void onSuccess(String response) {
                                if (response.contains("200")) {
                                    PreUtils.clearAll();
                                    SharedPreUtils.getInstance().clearAll();
                                    removeAllActivity();
                                    Intent intent = new Intent(AccountDelActivity.this, SplashActivity.class);
                                    startActivity(intent);
                                    FileUtils.deleteFile(Constant.BOOK_CACHE_PATH);
                                }
                            }

                            @Override
                            public boolean onFail(String response) {
                                return false;
                            }
                        });
                    }

                    @Override
                    public void onNoClick(Dialog dialog) {

                    }
                }).show();

            }
        });
    }

    @Override
    public void afterCreate() {
        post(Url.getUserProfile, null, UserProfileRequest.class, new FFNetWorkCallBack<UserProfileRequest>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(UserProfileRequest response) {
                if (response.getCode() == 200) {
                    tv_userInfo.setText("用户名:" + response.getData().getNickname() + "，客户号:" + response.getData().getUserNumber() + "，您好在您申请账号删除之前，请确认您的账户资产");
                    if (response.getData().getMonthSubscribeInfo() != null && response.getData().getMonthSubscribeInfo().getExpiredTime() > 0) {
                        tv_time.setText("包月剩余时间：" + FFUtils.getBetweenTimeDay(response.getData().getMonthSubscribeInfo().getExpiredTime()) + "天");
                    } else {
                        tv_time.setText("包月剩余时间：无");
                    }

                }


            }

            @Override
            public boolean onFail(UserProfileRequest response) {
                return false;
            }
        });
        post(Url.balance, null, UserBalanceResponse.class, new FFNetWorkCallBack<UserBalanceResponse>() {
            @Override
            public void onSuccess(UserBalanceResponse response) {
                if (response.getCode() == 200) {
                    tv_wjb.setText("完结币:" + response.getData().getBalance());

                }

            }

            @Override
            public boolean onFail(UserBalanceResponse response) {
                return false;
            }
        });
        post(Url.listUserBookSubscribe, null, BookPurchaseResponse.class, new FFNetWorkCallBack<BookPurchaseResponse>() {
            @Override
            public void onSuccess(BookPurchaseResponse response) {
                if (response.getCode() == 200) {
                    tv_buy.setText("已购书籍：" + response.getData().size() + "本");
                }
            }

            @Override
            public boolean onFail(BookPurchaseResponse response) {
                return false;
            }
        }, "{\"offset\":0,\"limit\":1000}");
    }

}
